﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Business;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using NHibernate.Criterion;
using ZLCMS.Commands.User.Commands.ProductCategory;

namespace ZLCMS.Web.SystemManage.Basics.ProductCategory
{
    public partial class CategoryInterface : System.Web.UI.Page
    {
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();


            //判断操作类型
            string opt = Request["opt"];
            if (opt == "load_data")
            {
                LoadData();
            }
            else if (opt == "update")
            {
                Update();
            }
            else if (opt == "update_ex")
            {
                UpdateEx();
            }
            else if (opt == "add")
            {
                Add();
            }
            else if (opt == "remove")
            {
                Remove();
            }
        }

        #region 加载产品类型列表 + public void LoadData()
        /// <summary>
        /// 加载产品类型列表
        /// </summary>
        public void LoadData()
        {
            LoadCategorysResult res = new LoadCategorysCommand().Execute(new LoadCategorysParameter()
            {
                Kind = "shop",
                OwnerAccount = _userAccount
            }).Result;
            Response.Write(res.CategoryList);
            Response.End();
        }
        #endregion

        #region 添加 + public void Add()
        /// <summary>
        /// 添加
        /// </summary>
        public void Add()
        {
            AddCategoryResult res = new AddCategoryCommand().Execute(new AddCategoryParameter()
            {
                CategoryName = Request["CategoryName"],
                ParentId = Request["ParentId"],
                ShortDesc = Request["ShortDesc"],
                OwnerAccount = _userAccount
            }).Result;
            Response.Write(res.category);
            Response.End();
        }
        #endregion

        #region 更新 + public void Update()
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            UpdateCategoryResult res = new UpdateCategoryCommand().Execute(new UpdateCategoryParameter()
            {
                Id = Request["Id"],
                CategoryName = Request["CategoryName"]
            }).Result;
            Response.Write(res.Category);
            Response.End();
        }
        #endregion

        #region 更新类别详细设置 + public void UpdateEx()
        /// <summary>
        /// 更新类别详细设置
        /// </summary>
        public void UpdateEx()
        {
            UpdateCategoryExResult res = new UpdateCategoryExCommand().Execute(new UpdateCategoryExParameter()
            {
                Id = Request["Id"],
                Sort = Request["Sort"],
                ShortDesc = Request["ShortDesc"],
                AttachmentUrl = Request["AttachmentUrl"],
                ImgUrl = Request["ImgUrl"],
                IsActive = Request["IsActive"],
                IsHot = Request["IsHot"],
                CategoryName = Request["CategoryName"]
            }).Result;
            Response.Write(res.Category);
            Response.End();
        }
        #endregion

        //#region 获取子节点 + public void GetChildNodeIds(IList<ShopCategory> list, int id, ref List<int> ids)
        ///// <summary>
        ///// 获取子节点ID
        ///// </summary>
        ///// <param name="list"></param>
        ///// <param name="id"></param>
        ///// <param name="ids"></param>
        //public void GetChildNodeIds(IList<ShopCategory> list, int id, ref List<int> ids)
        //{
        //    foreach (ShopCategory type in list)
        //    {
        //        if (type.Id == id)
        //        {
        //            ids.Add(type.Id);
        //        }
        //        if (type.ParentId == id)
        //        {
        //            GetChildNodeIds(list, type.Id, ref ids);
        //        }
        //    }
        //}
        //#endregion

        #region 删除 + protected void Remove()
        /// <summary>
        /// 删除
        /// </summary>
        protected void Remove()
        {
            var res = new RemoveCategoryCommand().Execute(new RemoveCategoryParameter()
            {
                Id = Request["Id"],
                OwnerAccount = _userAccount
            });

            if (res.Error)
            {
                Response.Write(res.ErrorMsg);
            }
            else
            {
                Response.Write(res.Result.Msg);
            }
            Response.End();
        }
        #endregion

    }
}